function wantedPage(p){
		$.ajax({
			url : "wanted/loadWanted.sls",
			type : "post",
			data : {"page":p},
			dataType : "json",
			success : function(result) {
				show(result);
			},
			error : function(){
				window.location.href="error.sls";
			}
		});
}

function show(result){

	$("#wanted_tbody").empty();
	if(result.status == 0){
		var wanteds = result.data;
		for(var i=0;i<wanteds.length;i++){
			var wId = wanteds[i].wId;
			var wJob = wanteds[i].wJob;
			var wNum = wanteds[i].wNum;
			var wPlace = wanteds[i].wPlace;
			var wDate = wanteds[i].wDate;
			var wTime = wanteds[i].wTime;
			var wState = wanteds[i].wState;
//			var deleteElement = '<a href="javascript:wanted_delet('+wId+');" class="ico del">删除</a>';
			var modifyElement = '<a href="javascript:wanted_modify('+wId+');" class="ico edit">完成</a>';
			var state;
			if(wState == '0'){
				state = "正在招聘";
			} else{
				modifyElement = "";
				state = "招聘完成";
			}
			
			
			
			
			var s = '<tr><td>'+wJob+'</td><td>'+
			wNum+'</td>'+
			'<td>'+wPlace+'</td>'+
			'<td>'+wDate+'</td>'+
			'<td>'+wTime+'</td>'+
			'<td>'+state+'</td>'+
			'<td>'+modifyElement+'</td>'+
			'</tr>';
			
			var $tr = $(s);
			$tr.data("wantedId",wId);
			$("#wanted_tbody").append($tr);
		}

		$.ajax({
			url : "wanted/wantedPage.sls",
			type : "post",
			dataType : "json",
			success : function(result) {
				if(result.status == 0 ){
					var page = result.data.page;
					var total = result.data.total;
					var top ;
					var end ;
					var pageBody = "";
					if(page == 1){
						top = '<a>上一页</a>';
					} else {
						top = '<a href="javascript:wantedPage('+(page-1)+');">上一页</a>';
					}
					if(page == total){
						end = '<a>下一页</a>';	
					} else {
						end = '<a href="javascript:wantedPage('+(page+1)+');">下一页</a>';
					}
					var i=1;
					var m=3;
					if(page>1){
						i = page-1;
						m = page+1;
					}
					if(m>total){
						i--;
						m = total;
					}
					if(i==0){
						i=1;
					}
					for(i;i<=m;i++){
						if(i==page){
							pageBody+='<a href="javascript:wantedPage('+i+');" style="font-weight:bold;color:#8d341f;">'+i+'</a>';
						} else {
							pageBody+='<a href="javascript:wantedPage('+i+');">'+i+'</a>';
						}
					}
					var s = top+pageBody+end;									
					$("#wanted_page").html(s);
				}
			},
			error : function(){
				window.location.href="error.sls";
			}
		});
	}
	

}

function wanted_modify(wantedId){
	$.ajax({
		url : "wanted/changeState.sls",
		type : "post",
		data : {"id":wantedId},
		dataType : "json",
		success : function(result) {
			if(result.status == 0){
				wantedPage(result.data);
			} 
			if(result.status == 2){
				$("#wanted_add_error").show();
				setTimeout(function(){$("#wanted_add_error").hide();},1000);
			}
		},
		error : function(){
			window.location.href="error.sls";
		}
	});
}

function searchName(){
	var name =$("#search").val();
	$.ajax({
		url : "wanted/searchWanted.sls",
		type : "post",
		data : {"name":name},
		dataType : "json",
		success : function(result) {
				show(result);
		},
		error : function(){
			window.location.href="error.sls";
		}
	});
}



